package com.sqj.proxyPattern.staticProxy;

import com.sqj.proxyPattern.DynamicDataSourceEntry;
import com.sqj.proxyPattern.IOrderService;
import com.sqj.proxyPattern.Order;
import com.sqj.proxyPattern.OrderServiceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;

public class OrderServiceStaticProxy implements IOrderService {

    private SimpleDateFormat yearFormat=new SimpleDateFormat("yyyy");

    private IOrderService orderService;

    public OrderServiceStaticProxy(OrderServiceImpl orderService){
        this.orderService=orderService;
    }

    @Override
    public void createOrder(Order order) {
        before();
        Date createDatetime = order.getCreateDatetime();
        Integer dbRoute=Integer.valueOf(yearFormat.format(createDatetime));
        DynamicDataSourceEntry.set(dbRoute);
        orderService.createOrder(order);
        after();
    }

    private void before(){
        System.out.println("代理开始");
    }

    private void after(){
        System.out.println("代理结束");
    }
}
